package com.cx.controller.lcd;

import com.cx.config.RequestResult;
import com.cx.pojo.Comment;
import com.cx.pojo.ResultData;
import com.cx.pojo.User;
import com.cx.service.lcd.CommentService;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.List;

@Controller
@ResponseBody
public class CommentController {
    @Autowired
    CommentService commentService;

    Calendar calendar = Calendar.getInstance();
    @RequestMapping("upsertcomment")
    public RequestResult upsertComment(HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        System.out.println(request.getParameter("commentContent"));

        String time = calendar.getTime().toString();
        Comment comment = new Comment();
        comment.setComment_id("5");
        comment.setComment_blog_id("01");
        comment.setComment_user_id(user.getUserId());
        comment.setComment_user_name(user.getUserName());
        //comment.setComment_user_img("http://tupiandizhi.com");
        comment.setComment_content(request.getParameter("commentContent"));
        comment.setComment_create_time(time);

        int affs=commentService.insertComment(comment);
        RequestResult requestResult=null;
        if(affs>0){
            requestResult=new RequestResult(1,"评论成功",comment);
        }else {
            requestResult=new RequestResult(2,"评论失败",comment);
        }
        return requestResult;
    }

    @RequestMapping("findall")
    @ResponseBody
    public ResultData findAllComments(){
        List<JSONObject> commentslist = commentService.findAllComments();
        ResultData resultData = new ResultData();
        if (null == commentslist || commentslist.size() == 0){
            resultData.setCode(3);
            resultData.setMessage("查不到数据");

        }else {
            resultData.setCode(0);
            resultData.setData(commentslist);
        }
        return resultData;

    }


}
